'''
Created on 2011-2-11

@author: ArcGIS
    http://webhelp.esri.com/arcgisserver/9.3/java/index.htm#geoprocessing/python_2126079299.htm
'''

#**********************************************************************
# Zips the contents of a folder.
# Parameters:
#   0 - Input folder.
#   1 - Output zip file. It is assumed that the user added the .zip 
#       extension.  
#
# Unzips the contents of a zip file into an existing folder.
# Arguments:
#  0 - Input zip file
#  1 - Input folder - pathname to existing folder that will contain 
#      the contents of the zip file. 
#**********************************************************************
import zipfile, os
from pwilib.util.plugin.schema.Compression import Compressor
from os.path import isdir, join, normpath, split

# Function for zipping files.  If keep is true, the folder, along with 
#  all its contents, will be written to the zip file.  If false, only 
#  the contents of the input folder will be written to the zip file - 
#  the input folder name will not appear in the zip file.
class PythonZip(Compressor):
    def zipw(self, path, zip, keep):
        path = os.path.normpath(path)
        # os.walk visits every subdirectory, returning a 3-tuple
        #  of directory name, subdirectories in it, and filenames
        #  in it.
        #
        for (dirpath, dirnames, filenames) in os.walk(path):
            # Iterate over every filename
            #
            for file in filenames:
                if keep:
                    zip.write(os.path.join(dirpath, file),
                              os.path.join(os.path.basename(path), os.path.join(dirpath, file)[len(path)+len(os.sep):]))
                else:
                    zip.write(os.path.join(dirpath, file),            
                              os.path.join(dirpath[len(path):], file)) 


    # Function to unzipping the contents of the zip file
    def unzip(self, path, zip):
        # If the output location does not yet exist, create it
        #
        if not isdir(path):
            os.makedirs(path)    
    
        for each in zip.namelist():
            # Check to see if the item was written to the zip file with an
            # archive name that includes a parent directory. If it does, create
            # the parent folder in the output workspace and then write the file,
            # otherwise, just write the file to the workspace.
            #
            if not each.endswith('/'): 
                root, name = split(each)
                directory = normpath(join(path, root))
                if not isdir(directory):
                    os.makedirs(directory)
                open(join(directory, name), 'wb').write(zip.read(each))


    def compress(self, path, zip_name, keep_top):
        zip = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)  
        self.zipw(path, zip, keep_top)
        return zip
    
def main():
    c = PythonZip()
    c.compress('F:/newSR', 'F:/newSR.zip', False).close()
    print('Done.')
    
if __name__ == '__main__':
    main()